var current_page = 1;
var maxPages;

$(function() {
    $('.row_four_slider').slider({
        min: 1,
        steep: 1,
        max: 5,
        range: true,
        values: [1, 5]
    });

    //get row_three
    getAdminHotels($('.row_one select').val(), $('.row_three select').val(), $('.row_two select').val(), $('.row_four_slider').slider("values"), current_page);

    $('.pages_list').on('click', 'ul li', function() {
        var page = $(this).text();
        if ($(this).text() !== '...') {
            changeAHotelPage(page);
        }
    });
    
    $('.hotel_search_button').on('click', function() {
        current_page = 1;
        getAdminHotels($('.row_one select').val(), $('.row_three select').val(), $('.row_two select').val(), $('.row_four_slider').slider("values"), current_page);
    });
});

function getAdminHotels(city_id, provider_id, hotel_id, arrstars, currentpage) {
    var object = {
        city: city_id,
        provider: provider_id,
        hotel: hotel_id,
        stars: arrstars,
        page: currentpage
    };

    $.ajax({
        type: "POST",
        url: url + 'searchadminhotel',
        data: object,
        beforeSend: function() {
            showHotelLoader();
        },
        success: function(msg) {
            var json = $.parseJSON(msg);
            hideHotelLoader();
            $(json).each(function(i, v) {
                insertHotels(v.hotels);
                insertPages(v.row);
            });
        }
    });
}

function insertHotels(hotels) {
    $('.hotels_container_line .user_line').remove();

    var mainDiv = $('.hotels_container_line');

    $(hotels).each(function(i, v) {
        var user_line = $('<div />').addClass('user_line');
        var user_line_id = $('<div />').addClass('user_line_id').html(v.id);
        var user_line_title = $('<div />').addClass('user_line_title').html(v.name);
        var img;
        if (parseInt(v.stars) === 1) {
            img = '/travis/web/images/icoStar.png';
        } else {
            img = '/travis/web/images/icoStars' + v.stars + '.png';
        }
        var user_line_image = $('<div />').addClass('hotels_admin_stars').append($('<img />').attr({'src': img}));
        var user_line_country = $('<div />').addClass('hotels_admin_country').html(v.country_name);
        var user_line_city = $('<div />').addClass('hotels_admin_city').html(v.city_name);
        var users_title_provider = $('<div />').addClass('hotels_admin_provider').html(v.provider_name);
        var user_line_edit_button = $('<div />').addClass('user_line_edit_button').click(function() {
            window.location.assign(urlHotelDetails + '/id/' + v.id);
        }).html('More');
        var user_line_delete_button = $('<div />').addClass('user_line_delete_button').click(function() {
            window.location.assign(urlDeleteHotel + '?hotel_id=' + v.id);
        }).html('Delete');

        (user_line).append(user_line_id).
                append(user_line_title).append(user_line_image).append(user_line_country).append(user_line_city).
                append(users_title_provider).append(user_line_edit_button).append(user_line_delete_button);

        (mainDiv).append(user_line);
    });
}

function insertPages(rows) {
    maxPages = Math.ceil(rows / 30);

    $('.pages_list ul').remove();
    
    if (parseInt(rows) === 0) {
        return;
    }

    var list = $('.pages_list');
    var ul = $('<ul />').append($('<li />').text('<<'));

    if (parseInt(maxPages) < 10) {
        for (var p = 0; p < maxPages; p++) {
            if (parseInt(current_page) === p + 1) {
                var li = $('<li />').addClass('active').text(p + 1);
            } else {
                var li = $('<li />').text(p + 1);
            }
            (ul).append(li);
        }
    } else {
        if (parseInt(current_page) < 6) {
            for (var p = 0; p < 9; p++) {
                if (parseInt(current_page) === p + 1) {
                    var li = $('<li />').addClass('active').text(p + 1);
                } else {
                    var li = $('<li />').text(p + 1);
                }
                (ul).append(li);
            }

            (ul).append($('<li />').text('...')).append($('<li />').text(maxPages));
        } else if (parseInt(maxPages) - parseInt(current_page) < 5) {
            (ul).append($('<li />').text(1)).append($('<li />').text('...'));
            for (var p = maxPages - 9; p < maxPages; p++) {
                if (parseInt(current_page) === p + 1) {
                    var li = $('<li />').addClass('active').text(p + 1);
                } else {
                    var li = $('<li />').text(p + 1);
                }
                (ul).append(li);
            }
        } else {
            (ul).append($('<li />').text(1)).append($('<li />').text('...'));
            for (var p = parseInt(current_page) - 5; p < parseInt(current_page); p++) {
                if (parseInt(current_page) === p + 1) {
                    var li = $('<li />').addClass('active').text(p + 1);
                } else {
                    var li = $('<li />').text(p + 1);
                }
                (ul).append(li);
            }

            for (var p = parseInt(current_page); p < (parseInt(current_page) + 4); p++) {
                (ul).append($('<li />').text(p + 1));
            }
            (ul).append($('<li />').text('...')).append($('<li />').text(maxPages));
        }
    }
    (ul).append($('<li />').text('>>'));
    (list).append(ul);
}

function changeAHotelPage(page) {
    if (page == '<<') {
        if (current_page > 1) {
            current_page--;

        }
    } else if (page == '>>') {
        if (current_page < maxPages) {
            current_page++;
        }
    } else {
        current_page = page;
    }

    getAdminHotels($('.row_one select').val(), $('.row_three select').val(), $('.row_two select').val(), $('.row_four_slider').slider("values"), current_page);
}

function showHotelLoader() {
    var winWidth = $(window).width();
    var winHeight = $(window).height();

    var boxWidth = 260;
    var disWidth = (winWidth - boxWidth) / 2;
    var disHeight = 270;
    $('.loader_hotel').css({'width': boxWidth + 'px', 'left': disWidth + 'px', 'top': disHeight + 'px'});
    $('.loader_hotel').show();
}

function hideHotelLoader() {
    $('.loader_hotel').hide();
}

//<?php
//                foreach ($hotels as $h) {
//                    $country = tblCountryTable::getInstance()->findByDql('id=?', $h->getCountryId())->getFirst();
//                    $city = tblCityTable::getInstance()->findByDql('id=?', $h->getCityId())->getFirst();
//                    $provider = tblProviderTable::getInstance()->findByDql('id=?', $h->getProviderId())->getFirst();
//
//                    if ($h->getStars() == 1) {
//                        $img = '<img src="/travis/web/images/icoStar.png" />';
//                    } else if ($h->getStars() == 2) {
//                        $img = '<img src="/travis/web/images/icoStars2.png" />';
//                    } else if ($h->getStars() == 3) {
//                        $img = '<img src="/travis/web/images/icoStars3.png" />';
//                    } else if ($h->getStars() == 4) {
//                        $img = '<img src="/travis/web/images/icoStars4.png" />';
//                    } else if ($h->getStars() == 5) {
//                        $img = '<img src="/travis/web/images/icoStars5.png" />';
//                    }
//
//                    echo '<div class="user_line">' .
//                    '<div class="user_line_id">' . $h->getId() . '</div>' .
//                    '<div class="user_line_title">' . $h->getNameEn() . '</div>' .
//                    '<div class="user_line_name">' . $img . '</div>' .
//                    '<div class="user_line_status">' . $country->getNameEn() . '</div>' .
//                    '<div class="user_line_permissions">' . $city->getNameEn() . '</div>' .
//                    '<div class="users_title_five">' . $provider->getShortName() . '</div>' .
//                    '<div class="user_line_edit_button"><a href="' . url_for('edithotel', array('hotel_id' => $h->getId())) . '">Edit</a></div>' .
//                    '<div class="user_line_delete_button"><a href="' . url_for('deletehotel', array('hotel_id' => $h->getId())) . '">' . 'Delete' . '</a></div>' .
//                    '</div>';
//                }
//                ?>

//<div class="pages_list">
//                    <ul>
//                        <li><<</li>
//                        <li class='active'>1</li>
//                        <li>>></li>
//                    </ul>
//                </div>